package com.beordie.service.impl;

import com.beordie.dao.IRoleDao;
import com.beordie.dao.IUsersDao;
import com.beordie.dao.impl.RoleDaoImpl;
import com.beordie.dao.impl.UsersDaoImpl;
import com.beordie.model.Menu;
import com.beordie.model.Role;
import com.beordie.model.Users;
import com.beordie.service.IUsersService;
import com.beordie.utils.DBUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Classname UsersDaoImpl
 * @Description TODO
 * @Date 2021/6/8 12:25
 * @Created 30500
 */
public class UsersServiceImpl implements IUsersService {
    private IUsersDao usersDao = new UsersDaoImpl();
    private IRoleDao roleDao = new RoleDaoImpl();
    @Override
    public Users getByNamePassword(String loginName, String password) {
        Users user = usersDao.getByNamePassword(loginName, password);
        if (user == null)
            return null;
        Role role = roleDao.getById(user.getRoleId());
        List<Menu> menuList = role.getMenu();
        List<Menu> newList = new ArrayList<>();

        // 循环遍历取出二级菜单填充
        for (Menu menu : menuList) {
            if (menu.getUpMenuId() == 0) {
                List<Menu> newTemp = new ArrayList<>();
                for (Menu menuS : menuList) {
                    if (menu.getMenuId() == menuS.getUpMenuId()) {
                        newTemp.add(menuS);
                    }
                }
                menu.setSecond(newTemp);
                newList.add(menu);
            }
        }
        // 设置二级菜单
        role.setMenu(newList);
        user.setRole(role);

        return user;
    }

    @Override
    public int getByAll() {
        return this.usersDao.getByAll();
    }

    @Override
    public List<Users> getByLimit(int index, int pageSize) {
        return usersDao.getByLimit(index, pageSize);
    }

    @Override
    public int add(Users user) {
        return usersDao.add(user);
    }

    @Override
    public int update(Users user) {
        return usersDao.update(user);
    }

    @Override
    public int delete(int index) {
        return usersDao.delete(index);
    }

    @Override
    public Users getByIndex(int userId) {
        return usersDao.getByIndex(userId);
    }
}
